package com.example.lotterysystem.controller;

import com.example.lotterysystem.common.Result;
import com.example.lotterysystem.common.enums.GlobalErrorCodeEnum;
import com.example.lotterysystem.common.exception.GlobalException;
import com.example.lotterysystem.common.utils.JacksonUtil;
import com.example.lotterysystem.controller.param.ActivityCreateParam;
import com.example.lotterysystem.controller.param.PageParam;
import com.example.lotterysystem.controller.result.ActivityCreateResult;
import com.example.lotterysystem.controller.result.ActivityDetailResult;
import com.example.lotterysystem.controller.result.ActivityListResult;
import com.example.lotterysystem.service.ActivityService;
import com.example.lotterysystem.service.dto.ActivityCreateDTO;
import com.example.lotterysystem.service.dto.ActivityDTO;
import com.example.lotterysystem.service.dto.ActivityDetailDTO;
import com.example.lotterysystem.service.dto.PageListDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;

@Slf4j
@RestController
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    @RequestMapping("/activity/create")
    public Result<ActivityCreateResult> createActivity(@Validated @RequestBody ActivityCreateParam param){
        log.info("ActivityController createActivity: {}", JacksonUtil.writeValueAsString(param));
        ActivityCreateDTO createDTO = activityService.createActivity(param);
        return Result.success(transfer(createDTO));
    }

    private ActivityCreateResult transfer(ActivityCreateDTO createDTO) {
        if(createDTO == null){
            throw new GlobalException(GlobalErrorCodeEnum.ACTIVITY_CREATE_ERROR);
        }
        ActivityCreateResult activityCreateResult = new ActivityCreateResult();
        activityCreateResult.setActivityId(createDTO.getActivityId());
        return activityCreateResult;
    }

    @RequestMapping("/activity/getActivityList")
    public Result<ActivityListResult> getActivityList(PageParam param){
        log.info("ActivityController getActivity: {}", JacksonUtil.writeValueAsString(param));
        PageListDTO<ActivityDTO> pageListDTO = activityService.getActivityList(param);
        return Result.success(transfer(pageListDTO));
    }
    private ActivityListResult transfer(PageListDTO<ActivityDTO> pageListDTO){
        if (pageListDTO == null){
            throw new GlobalException(GlobalErrorCodeEnum.ACTIVITY_LIST_ERROR);
        }
        ActivityListResult result = new ActivityListResult();
        result.setTotal(pageListDTO.getTotal());
        result.setRecords(pageListDTO.getRecords().stream()
                .map(activityDTO -> {
                    ActivityListResult.ActivityInfo activityInfo = new ActivityListResult.ActivityInfo();
                    activityInfo.setActivityId(activityDTO.getActivityId());
                    activityInfo.setActivityName(activityDTO.getActivityName());
                    activityInfo.setDescription(activityDTO.getDescription());
                    activityInfo.setValid(activityDTO.valid());
                    return activityInfo;
                }).toList());
        return result;
    }

    @RequestMapping("/activity/getActivityDetail")
    public Result<ActivityDetailResult> getActivityDetail(Long activityId){
        log.info("ActivityController getActivityDetail: {}", activityId);
        ActivityDetailDTO activityDetailDTO = activityService.getActivityDetail(activityId);
        return Result.success(transfer(activityDetailDTO));
    }

    private ActivityDetailResult transfer(ActivityDetailDTO detailDTO){
        if (detailDTO == null){
            throw new GlobalException(GlobalErrorCodeEnum.ACTIVITY_DETAIL_ERROR);
        }
        ActivityDetailResult result = new ActivityDetailResult();
        result.setActivityId(detailDTO.getActivityId());
        result.setActivityName(detailDTO.getActivityName());
        result.setDescription(detailDTO.getDescription());
        result.setValid(detailDTO.valid());
        //  抽奖顺序 3 2 1
        result.setPrizes(detailDTO.getPrizeList().stream()
                .sorted(Comparator.<ActivityDetailDTO.PrizeDTO>comparingInt(priceDTO ->
                        priceDTO.getPrizeGrade().getCode()
                ).reversed())
                .map(prizeDTO -> {
                    ActivityDetailResult.PrizeDetail prize = new ActivityDetailResult.PrizeDetail();
                    prize.setPrizeId(prizeDTO.getPrizeId());
                    prize.setName(prizeDTO.getPrizeName());
                    prize.setDescription(prize.getDescription());
                    prize.setPrice(prizeDTO.getPrice());
                    prize.setImageUrl(prizeDTO.getImageUrl());
                    prize.setPrizeAmount(prizeDTO.getPrizeAmount());
                    prize.setPrizeTierName(prizeDTO.getPrizeGrade().getValue());
                    prize.setValid(prizeDTO.valid());
                    return prize;
                }).toList());
        result.setUsers(detailDTO.getUserList().stream()
                .map(userDTO -> {
                    ActivityDetailResult.UserDetail user = new ActivityDetailResult.UserDetail();
                    user.setUserId(userDTO.getUserId());
                    user.setUserName(userDTO.getUserName());
                    user.setValid(userDTO.valid());
                    return user;
                }).toList());
        return result;
    }
}
